#coding=UTF-8
from PyQt4 import QtCore, QtGui
from Information_Centre_ImageChar import *
import IC_DefineLibrary
import IC_VariableLibrary
from Register_UserInformation import *

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Login_Dialog(QtGui.QMainWindow): #object    
    def setupUi(self):
        QtGui.QWidget.__init__(self)
        global VAR
        VAR=[]
        self.setObjectName(_fromUtf8("Dialog")) #Dialog
        self.resize(500, 330)
        self.frame_1 = QtGui.QFrame(self)
        self.frame_1.setGeometry(QtCore.QRect(0, 0, 500, 330))
        self.frame_1.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_1.setFrameShadow(QtGui.QFrame.Raised)
        self.frame_1.setObjectName(_fromUtf8("frame_1"))
        self.frame_2 = QtGui.QFrame(self.frame_1)
        self.frame_2.setGeometry(QtCore.QRect(0, 0, 500, 70))
        self.frame_2.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_2.setFrameShadow(QtGui.QFrame.Raised)
        self.frame_2.setObjectName(_fromUtf8("frame_2"))
        self.label_1 = QtGui.QLabel(self.frame_2)
        self.label_1.setGeometry(QtCore.QRect(0, 0, 500, 70))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.label_1.setFont(font)
        self.label_1.setFrameShape(QtGui.QFrame.StyledPanel)
        self.label_1.setAlignment(QtCore.Qt.AlignCenter)
        self.label_1.setObjectName(_fromUtf8("label_1"))
        self.frame_3 = QtGui.QFrame(self.frame_1)
        self.frame_3.setGeometry(QtCore.QRect(0, 70, 500, 240))
        self.frame_3.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_3.setFrameShadow(QtGui.QFrame.Raised)
        self.frame_3.setObjectName(_fromUtf8("frame_3"))
        self.label_2 = QtGui.QLabel(self.frame_3)
        self.label_2.setGeometry(QtCore.QRect(90, 30, 100, 30))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.label_3 = QtGui.QLabel(self.frame_3)
        self.label_3.setGeometry(QtCore.QRect(90, 80, 100, 30))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.label_3.setFont(font)
        self.label_3.setAlignment(QtCore.Qt.AlignCenter)
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.textEdit_1 = QtGui.QTextEdit(self.frame_3)
        self.textEdit_1.setGeometry(QtCore.QRect(190, 30, 200, 30))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setPointSize(9)
        self.textEdit_1.setFont(font)
        self.textEdit_1.setTabChangesFocus(True)
        self.textEdit_1.setObjectName(_fromUtf8("textEdit_1"))
        self.lineEdit_1 = QtGui.QLineEdit(self.frame_3)
        self.lineEdit_1.setGeometry(QtCore.QRect(190, 80, 200, 30))
        self.lineEdit_1.setEchoMode(QtGui.QLineEdit.Password)
        self.lineEdit_1.setObjectName(_fromUtf8("lineEdit_1"))
        self.textEdit_2 = QtGui.QTextEdit(self.frame_3)
        self.textEdit_2.setGeometry(QtCore.QRect(190, 130, 200, 30))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setPointSize(9)
        self.textEdit_2.setFont(font)
        self.textEdit_2.setTabChangesFocus(True)
        self.textEdit_2.setObjectName(_fromUtf8("textEdit_2"))
        ic1 = ImageChar(fontColor=(100,211, 90))
        VAR=ic1.randChinese(4)
        ic1.save("1.jpeg")
#        print '1.jpg',VAR
        self.label_4 = IC_DefineLibrary.labelBtn(self.frame_3,QtGui.QPixmap(_fromUtf8("1.jpeg")))#, QtGui.QPixmap(_fromUtf8("2.jpeg")))
        self.label_4.setGeometry(QtCore.QRect(85, 125, 100, 40))
        self.label_4.setText(_fromUtf8(""))
#        self.label_4.setPixmap(QtGui.QPixmap(_fromUtf8("1.jpeg"))) 
        self.label_4.setAlignment(QtCore.Qt.AlignCenter)
        self.label_4.setObjectName(_fromUtf8("label_4"))
        QtCore.QObject.connect(self.label_4, QtCore.SIGNAL("pressed()"),self.ChangeIdentification)
        self.pushButton_1 = QtGui.QPushButton(self.frame_3)
        self.pushButton_1.setGeometry(QtCore.QRect(190, 190, 80, 30))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_1.setFont(font)
        self.pushButton_1.setObjectName(_fromUtf8("pushButton_1"))
#        print 'setup',VAR  
        QtCore.QObject.connect(self.pushButton_1, QtCore.SIGNAL("clicked()"),self.Info_Check)
        self.pushButton_2 = QtGui.QPushButton(self.frame_3)
        self.pushButton_2.setGeometry(QtCore.QRect(295, 190, 80, 30))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL("clicked()"),self.UserRegister )
        self.pushButton_3 = QtGui.QPushButton(self.frame_3)
        self.pushButton_3.setGeometry(QtCore.QRect(400, 190, 80, 30))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_3.setFont(font)
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        QtCore.QObject.connect(self.pushButton_3, QtCore.SIGNAL("clicked()"), IC_VariableLibrary.app.closeAllWindows)
        self.frame_4 = QtGui.QFrame(self.frame_1)
        self.frame_4.setGeometry(QtCore.QRect(0, 310, 500, 20))
        self.frame_4.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_4.setFrameShadow(QtGui.QFrame.Plain)
        self.frame_4.setObjectName(_fromUtf8("frame_4"))
        
        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslateUi(self):  #Dialog
        self.setWindowTitle(_translate("Dialog", "Information Centre", None))
        self.label_1.setText(_translate("Dialog", "Information Centre", None))
        self.label_2.setText(_translate("Dialog", "Username", None))
        self.label_3.setText(_translate("Dialog", "Password", None))
        self.pushButton_1.setText(_translate("Dialog", "Ok", None))
        self.pushButton_2.setText(_translate("Dialog", "Register", None))
        self.pushButton_3.setText(_translate("Dialog", "Close", None))
    
    def ChangeIdentification(self):
        global VAR
        ic2 = ImageChar(fontColor=(100,211, 90))
        VAR=ic2.randChinese(4)
        ic2.save("2.jpeg")
        self.label_4.setPixmap(QtGui.QPixmap(_fromUtf8("2.jpeg")))
    
    def Info_Check(self):
        global VAR
        te1=self.textEdit_2.toPlainText()
        varcon="".join(VAR) 
#        print 'varcon',varcon, 'setup',VAR,te1  
        if self.textEdit_1.toPlainText() and self.lineEdit_1.text():
            if varcon != te1:
                self.textEdit_2.setPlainText("")
                IC_DefineLibrary.MessageBox(title='Notes',message='Sorry, you have input wrong chinese identify code, please try again !')
                return IC_DefineLibrary.MessageBox(title='Notes',message='You could change the chinese identify code picture by clicking that picture, then please try again !')
        else:
            return IC_DefineLibrary.MessageBox(title='Notes',message='Sorry, please input username and password integrallty !') 
        dbo1=IC_DefineLibrary.DB_Operating(sql="Select Username,Password,Login_Succeed_Counts,Login_Fail_Counts from Login lg inner join User_Login_Counts ulc on lg.Username=ulc.English_Name")
        for Username,Password,Login_Succeed_Counts,Login_Fail_Counts in dbo1.row:
            if self.textEdit_1.toPlainText() ==Username:
                if self.lineEdit_1.text()==Password:
                    IC_VariableLibrary.FLAG=1
#                    print IC_VariableLibrary.FLAG
                    IC_VariableLibrary.COUNT=0
                    Login_Succeed_Counts+=1
                    self.textEdit_1.setPlainText("")
                    self.textEdit_2.setPlainText("")
                    self.lineEdit_1.clear()
#                    mainmenu()
                else:
                    Login_Fail_Counts+=1
                dbo2=IC_DefineLibrary.DB_Operating(sql="update User_Login_Counts set Login_Succeed_Counts="+"%d"%Login_Succeed_Counts+", Login_Fail_Counts="+"%d"%Login_Fail_Counts+" where English_Name='"+"%s"%Username+"'")
        if IC_VariableLibrary.FLAG==0 and IC_VariableLibrary.COUNT==0:
            IC_DefineLibrary.MessageBox(title='Notes',message='Wrong Username and/or Password ! You have tried 1 times, please try again !')
        elif IC_VariableLibrary.FLAG==0 and IC_VariableLibrary.COUNT==1:
            IC_DefineLibrary.MessageBox(title='Notes',message='Wrong Username and/or Password ! You have tried 2 times, please try again ! Be careful: you just have the last chance ! ')
        elif IC_VariableLibrary.FLAG==0 and IC_VariableLibrary.COUNT==2:
            IC_VariableLibrary.app.closeAllWindows()
            import ctypes
            dll = ctypes.WinDLL('user32.dll')
            dll.LockWorkStation()
        if IC_VariableLibrary.FLAG==0:
            self.textEdit_1.setPlainText("")
            self.lineEdit_1.clear()
            IC_VariableLibrary.COUNT+=1
    
    def UserRegister(self):
        IC_VariableLibrary.FLAG=0
        self.myapps1=Ui_Register_Dialog()
        self.myapps1.setupUi1()
        self.myapps1.show()

